<template>
  <div>
    <div class="action-bar">
      <el-button type="primary" size="mini" icon="el-icon-plus" @click="add">新建</el-button>
      <el-button v-if="isShow" size="mini" icon="el-icon-upload2" @click="uploadDialogFormVisible = true">导入</el-button>
      <el-button size="mini" icon="el-icon-download" @click="exportExcel">导出</el-button>
    </div>
    <FormDialog title="新建" :show="dialogFormVisible" width="40%" @on-ok="ok" @on-cancel="cancel">
      <EditForm ref="editForm" />
    </FormDialog>
    <FormDialog title="导入" :show="uploadDialogFormVisible" width="40%" @on-ok="closeDialog" @on-cancel="closeDialog">
      <UploadNavigation :download-funct="template" file-name="配件上传模板" :action="importExcelUrl" :upload-success="handleSuccess" />
    </FormDialog>
  </div>
</template>
<script>
import FormDialog from '@neu/biz-core/components/FormDialog'
import UploadNavigation from '@neu/biz-core/components/UploadNavigation'
// 编辑实例组件独立编写，需要传递id属性，判断新建还是更新操作
import EditForm from './editForm'
import { importExcelUrl, template, exportExcel } from '@neu/dcam-ui/api/goodsFile'
import { userRole } from '@neu/dcam-ui/api/fixedAsset'

export default {
  name: 'GoodsFileActionBar',
  components: {
    FormDialog,
    EditForm,
    UploadNavigation
  },
  data() {
    return {
      dialogUploadVisible: false,
      dialogFormVisible: false,
      importExcelUrl: importExcelUrl,
      template: template,
      uploadDialogFormVisible: false,
      isShow: true
    }
  },
  created() {
    userRole().then(response => {
      if (!response.data.show) {
        this.isShow = false
      }
    })
  },
  methods: {
    // 新建用户按钮点击处理
    add() {
      this.dialogFormVisible = true
    },
    // 创建用户对话框确认事件处理
    ok() {
      // 直接调用表单实例的保存方法
      this.$refs.editForm.save().then(response => {
        if (response === false) return
        this.$message({
          showClose: true,
          message: '操作成功！',
          type: 'success'
        })
        // 调用pageable-table实例的mixin方法refresh()，更新表格
        this.$parent.refresh()
        // 关闭对话框
        this.dialogFormVisible = false
        this.$refs.editForm.reset()
      })
    },
    cancel() {
      this.$refs.editForm.reset()
      this.dialogFormVisible = false
    },
    exportExcel() {
      exportExcel().then(res => {
        const content = res.data
        const blob = new Blob([content])
        const fileName = '配件信息.xlsx'
        if ('download' in document.createElement('a')) { // 非IE下载
          const elink = document.createElement('a')
          elink.download = fileName
          elink.style.display = 'none'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href) // 释放URL 对象
          document.body.removeChild(elink)
        } else { // IE10+下载
          navigator.msSaveBlob(blob, fileName)
        }
      })
    },
    handleSuccess(response) {
      this.uploadDialogFormVisible = false
      this.$parent.refresh()
    },
    closeDialog() {
      this.uploadDialogFormVisible = false
      this.$parent.refresh()
    }
  }

}
</script>
